<?php 
class Helper_Page
{
    // IE地址栏地址
    private $url;
    // 记录总条数
    private $countall;
    // 总页数
    private $page;
    // 分页数字链接
    private $thestr;
    // 首页、上一页链接
    private $backstr;
    // 尾页、下一页链接
    private $nextstr;
    // 当前页码
    private $pg;
    // 每页显示记录数量
    private $countlist;
    // 翻页样式
    private $style;
    //上一页
    private $gotoback;
    
    private $param;
    
    // 构造函数，实例化该类的时候自动执行该函数
    function __construct($countall, $countlist, $param,$style = "page")
    {
        $this->countall = $countall;
        $this->countlist = $countlist;
        $this->style = $style;
        $this->param=$param;
        // 记录数与每页显示数不能整队时，页数取余后加1
        if ($this->countall % $this->countlist != 0) {
            $this->page = sprintf("%d", $this->countall / $this->countlist) + 1;
        } else {
            $this->page = $this->countall / $this->countlist;
        }
        if(isset($_GET["pg"])){
            $this->pg = $_GET["pg"];
        }
        // 保证pg在未指定的情况下为从第1页开始
        if (! preg_match("/^[1-9][0-9]*$/", $this->pg) || empty($this->pg)) {
            $this->pg = 1;
        }
        // 页码超出最大范围，取最大值
        if ($this->pg > $this->page) {
            $this->pg = $this->page;
        }
        // 得到当前的URL。具体实现请看最底部的函数实体
        $this->url = $this->getUrl();
        // 替换错误格式的页码为正确页码
        if (isset($_GET["pg"]) && $_GET["pg"] != $this->pg) {
            $this->url = str_replace("?pg=" . $_GET["pg"], "?pg=$this->pg", 
                    $this->url);
            $this->url = str_replace("&pg=" . $_GET["pg"], "&pg=$this->pg", 
                    $this->url);
        }
        // 生成12345等数字形式的分页。
        if ($this->page <= 8) {
            for ($i = 1; $i < $this->page + 1; $i ++) {
                $this->thestr = $this->thestr . $this->makepg($i, $this->pg);
            }
        
        } else {
            if ($this->pg <= 5) {
                for ($i = 1; $i < 9; $i ++) {
                    $this->thestr = $this->thestr . $this->makepg($i, $this->pg);
                }
            } else {
                if (5 + $this->pg <= $this->page) {
                    for ($i = $this->pg - 3; $i < $this->pg + 5; $i ++) {
                        $this->thestr = $this->thestr . $this->makepg($i, $this->pg);
                            }
                        } else {
                            for ($i = $this->pg - 7; $i < $this->page + 1; $i ++) {
                                $this->thestr = $this->thestr . $this->makepg($i, $this->pg);
                                    }
                                }
                            }
                        }
                        // 生成上页下页等文字链接
                        $this->backstr = $this->gotoback($this->pg);
                        $this->nextstr = $this->gotonext($this->pg, $this->page);
                        echo ("<b class='pageinfo' style='font-size:14px;display:inline'>  共  " . $this->countall . " 条   &nbsp;&nbsp;" .'</b>'. $this->backstr .
                                 $this->thestr . $this->nextstr);
            }
    //生成数字分页的辅助函数 
     function makepg($i,$pg){ 
     if ($i==$pg){ 
            return " <font style='font-size:14px float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;color:#333;background:#a9d2ff; padding:0px 6px; font-weight:bold;text-decoration:none;display:inline' class='".$this->style."'>&nbsp;".$i."&nbsp;&nbsp;</font>"; 
        }else{ 
            return " <a style='font-size:14px float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;color:#333; padding:0px 6px; background:#e6f2fe;text-decoration:none;display:inline' href=".$this->replacepg($this->url,5,$i)." class='".$this->style." '>&nbsp;&nbsp;".$i."&nbsp;&nbsp;</a>"; 
        } 
    } 
    //生成上一页等信息的函数 
     function gotoback($pg){ 
        if ($pg-1>0){ 
            return $this->gotoback=" <a style='font-size:14px float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;color:#333; text-decoration:none;padding:0px 6px; background:#e6f2fe;display:inline' href=".$this->replacepg($this->url,3,0)." class='".$this->style."'>首页</a> <a style='font-size:14px float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;color:#333; text-decoration:none;padding:0px 6px; background:#e6f2fe;display:inline' href=".$this->replacepg($this->url,2,0)." class='".$this->style."'>上一页</a>"; 
        }else{ 
            return $this->gotoback="<span style='font-size:14px float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;color:#333; text-decoration:none;padding:0px 6px; background:#e6f2fe;display:inline' class='".$this->style."'>首页</span><span style='font-size:14px float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;color:#333; text-decoration:none;padding:0px 6px; background:#e6f2fe;display:inline' class='".$this->style."'> 上一页 </span>"; 
        } 
     } 
    //生成下一页等信息的函数 
     function gotonext($pg,$page){ 
        if ($pg < $page){ 
            return " <a style='font-size:14px float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;color:#333; text-decoration:none;padding:0px 6px; background:#e6f2fe;display:inline'  href=".$this->replacepg($this->url,1,0)." class='".$this->style."'>下一页&nbsp;</a> <a style='font-size:14px float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;color:#333; text-decoration:none;padding:0px 6px; background:#e6f2fe;display:inline' href=".$this->replacepg($this->url,4,0)." class='".$this->style."'>尾页</a>"; 
        }else{ 
            return " <span style='font-size:14px float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;color:#333; text-decoration:none;padding:0px 6px; background:#e6f2fe;display:inline'  '>下一页</span><span style='font-size:14px float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;color:#333; text-decoration:none;padding:0px 6px; background:#e6f2fe;display:inline' class='".$this->style." '>尾页</span>"; 
        } 
    } 
    //处理url中$pg的方法,用于自动生成pg=x 
     function replacepg($url,$flag,$i){ 
        if ($flag == 1){ 
            $temp_pg = $this->pg; 
            return str_replace("pg=".$temp_pg,"pg=".($this->pg+1),$url); 
        }else if($flag == 2) { 
            $temp_pg = $this->pg; 
            return str_replace("pg=".$temp_pg,"pg=".($this->pg-1),$url); 
        }else if($flag == 3) { 
            $temp_pg = $this->pg; 
            return str_replace("pg=".$temp_pg,"pg=1",$url); 
        }else if($flag == 4){ 
            $temp_pg = $this->pg; 
            return str_replace("pg=".$temp_pg,"pg=".$this->page,$url); 
        }else if($flag == 5){ 
            $temp_pg = $this->pg; 
        return str_replace("pg=".$temp_pg,"pg=".$i,$url); 
        }else{ 
            return $url; 
        } 
    } 
    //获得当前URL的方法 
     function getUrl(){ 
        $url='';
        if(isset($_SERVER["REQUEST_URI"])){ 
            $url.=$_SERVER["REQUEST_URI"]; 
        }else{ 
            $url.=$_SERVER["PHP_SELF"]; 
        if(!empty($_SERVER["QUERY_STRING"])){ 
            $url.="?".$_SERVER["QUERY_STRING"]; 
        } 
    } 
    //在当前的URL里加入pg=x字样 
        if (!strpos($url,"?")){ 
            $url = $url."?pg=1";           
        }else{ 
            if(!strpos($url,"&pg")){
                $url = $url."&pg=1";                
            }else{           
            }
        } 
        $param=$this->param;       
        if(!empty($param)){
            foreach( $param as $k=>$v){
                 if(!strpos($url,"&".$k."=")){
                 if($v)$url.="&".$k."=".$v;
                 }
            }
        }   
        return $url; 
    } 
} 
?>